<?php
require_once 'Validate.php';
class RegisterView {
	private $username = "username";
	private $password = "password";
	private $passwordCheck = "passwordcheck";
	private $email = "email";
	private $registerButton = "register";
	private $cancelButton = "cancel";
	public $errormessage;

	public function createRegisterBox() {
		$status = "<p class='status'>Registrering</p>";
		$inputUsername = NULL;
		if (isset($_POST['username'])) {
			$inputUsername = $_POST['username'];
		}
		$registerbox = "<form action='' method='post' class='registerform'>
					<fieldset>
						<label for='$this->username'>Användarnamn:</label> <input type='text' id='$this->username' name='$this->username'  value='$inputUsername'/> <br /> 
						<label for='$this->password'>Lösenord:</label> <input type='password'  id='$this->password' name='$this->password' /> <br />
						<label for='$this->passwordCheck'>Upprepa lösenord:</label> <input type='password'  id='$this->passwordCheck' name='$this->passwordCheck' /> <br />
						<input type='submit' name='$this->registerButton' value='Skicka' />
					</fieldset>
					</form>";
		return $status . $registerbox;
		// <input type='submit' name='$this->cancelButton' value='Avbryt' />
		// <label for='$this->email'>Email:</label> <input type='password'  id='$this->email' name='$this->email' /> <br />
	}

	public function registerSuccess() {
		$status = "<p class='status'>Registrering</p>";
		$successmsg = "<p>Registreringen lyckades! Nu kan du <a href='/lab3'>logga in</a>.";
		return $status . $successmsg;
	}

	public function showFeedback($action) {
		// Returns passed value enclosed by paragraph tags
		$msg = "<p class='error'>$action</p>";
		return $msg;
	}

	public function triedToRegister() {
		// Returns true if login button has been clicked
		return isset($_POST[$this -> registerButton]);
	}

	public function triedToCancel() {
		// Returns true if login button has been clicked
		return isset($_POST[$this -> cancelButton]);
	}

	public function getUserName() {
		// Get and return username if input is valid
		$validate = new validate();
		if ($_POST[$this -> username] == null) {
			// Show feedback and return false if there's no username input
			Feedback::addError(Feedback::noUsername);
			return false;
		}
		if (!$validate -> validateUsername($_POST[$this -> username])) {
			// Show feedback and return false if username is not valid
			Feedback::addError(Feedback::wrongUsername);
			return false;
		}
		return $_POST[$this -> username];
	}

	public function getPassword() {
		// Get and return password if input is valid
		$validate = new validate();
		if ($_POST[$this -> password] == null) {
			// Show feedback and return false if there's no password input
			Feedback::addError(Feedback::noPassword);
			return false;
		}
		if (!$validate -> validatePassword($_POST[$this -> password])) {
			// Show feedback and return false if password is not valid
			Feedback::addError(Feedback::wrongPassword);
			return false;
		}
		if ($_POST[$this -> passwordCheck] !== $_POST[$this -> password]) {
			// Show feedback and return false if passwords doesn't match
			Feedback::addError(Feedback::unmatchedPassword);
			return false;
		}
		return $_POST[$this -> password];
	}

	public function getPasswordCheck() {
		$validate = new validate();
		// Get and return username, if name has been input...
		if (isset($_POST[$this -> passwordCheck]) == true) {
			return $_POST[$this -> passwordCheck];
		}
	}

}
